//
//  PrepaidPhoneHomePageVC.m
//  ManageMoney
//
//  Created by 大明 on 16/4/21.
//  Copyright © 2016年 db. All rights reserved.
//

#import "PrepaidPhoneHomePageVC.h"
#import "CardManagement.h"
#import "PayMethodsController.h"
#import "WithdrawalVC.h"
#import "PrepaidPhoneHomePageCell.h"
@interface PrepaidPhoneHomePageVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray *PrepaidPhoneHomePageArray;
    NSString *PrepaidPhoneHomePageStr;
}
@property (weak, nonatomic) IBOutlet UITableView *PrepaidTableView;

@end

@implementation PrepaidPhoneHomePageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _PrepaidTableView.delegate = self;
    _PrepaidTableView.dataSource = self;
    //footview
    UIView *footerView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
    _PrepaidTableView.tableFooterView = footerView2;
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self PrepaidPhoneHomePage];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

-(void)PrepaidPhoneHomePage
{
    PrepaidPhoneHomePageArray = [[NSMutableArray alloc]init];
    [SVProgressHUD show];
    NSString *PrepaidPhoneHomePageUrl = [@"" stringByAppendingFormat:@"%@%@",hrtxUrl,@"fund/queryCustChannelList.do"];
    NSMutableDictionary *pramas  = [[NSMutableDictionary alloc]init];
    pramas[@"custNo"] = CheckNull(UserInfo[@"KHBH"]);
    
    [HWHttpTool post:PrepaidPhoneHomePageUrl params:pramas success:^(id json) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",json);
        
        NSDictionary *dic = json;
        if ([dic[@"responseCode"] intValue] == 200) {
            
            PrepaidPhoneHomePageArray = dic[@"responseObj"];
            
            [_PrepaidTableView reloadData];
            
            
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - tableview的代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [PrepaidPhoneHomePageArray count];
    
}

//修改tableview的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    static NSString *indentifier = @"cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
//    if (cell==nil) {
//        //        cell = [[NSBundle mainBundle] loadNibNamed:@"PayHistoryCell" owner:self options:nil][0];
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
//    }
    
    static NSString *indentifier = @"PrepaidPhoneHomePageCell";
    PrepaidPhoneHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (cell==nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"PrepaidPhoneHomePageCell" owner:self options:nil][0];
    }
    
    NSDictionary *dic = PrepaidPhoneHomePageArray[indexPath.row];
    PrepaidPhoneHomePageStr = CheckNull(dic[@"customerNum"]);
        //取消点击效果
//        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.iconImage.image = [UIImage imageNamed:@"ic_reapal"];
    cell.nameLabel.text = CheckNull(dic[@"channelSummary"]);
//    
    cell.moneyLabel.text = [NSString stringWithFormat:@"%@",CheckNull(dic[@"accountBalance"])];
    cell.tixianLabel.text = @"提现";
    
        
        
        return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = PrepaidPhoneHomePageArray[indexPath.row];
    if ([CheckNull(dic[@"accountBalance"]) doubleValue] < 10) {
        [SVProgressHUD showErrorWithStatus:@"你的账户余额低于10元，无法提现"];
        return;
    }
    WithdrawalVC *Withdrawal = [[WithdrawalVC alloc]initWithNibName:@"WithdrawalVC" bundle:nil];
    Withdrawal.WithdrawalStr = CheckNull(dic[@"channelNum"]);
    [self.navigationController pushViewController:Withdrawal animated:YES];
}

//充值按钮
- (IBAction)topupClick:(id)sender {
    CardManagement *CreditCard = [[CardManagement alloc]initWithNibName:@"CardManagement" bundle:nil];
    CreditCard.addCardStr = @"2";
    CreditCard.customerNoStr = PrepaidPhoneHomePageStr;
    [self.navigationController pushViewController:CreditCard animated:YES];

}
//交易记录按钮
- (IBAction)TransactionRecordsClick:(id)sender {
    PayMethodsController *ctr = [[PayMethodsController alloc] initWithNibName:@"PayMethodsController" bundle:nil];
    ctr.title = @"账户查询";
    ctr.payType = 2;
    [self.navigationController pushViewController:ctr animated:YES];
}
//信用卡认证
- (IBAction)TheCreditCardAuthenticationClick:(id)sender {
    
     CardManagement *CreditCard = [[CardManagement alloc]initWithNibName:@"CardManagement" bundle:nil];
    CreditCard.addCardStr = @"2";
    CreditCard.customerNoStr = PrepaidPhoneHomePageStr;
    [self.navigationController pushViewController:CreditCard animated:YES];
}
//储蓄卡认证
- (IBAction)CashCardCertificationClick:(id)sender {
    CardManagement *CashCardCertification = [[CardManagement alloc]initWithNibName:@"CardManagement" bundle:nil];
    CashCardCertification.addCardStr = @"1";
    CashCardCertification.customerNoStr = PrepaidPhoneHomePageStr;
    [self.navigationController pushViewController:CashCardCertification animated:YES];
}
//返回
- (IBAction)backClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
